package cn.com.jrr.core.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.jrr.core.common.WebKey;
import cn.com.jrr.core.model.ResponseMessage;
import cn.com.jrr.core.model.power.SysResource;
import cn.com.jrr.core.model.power.SysUser;
import cn.com.jrr.core.model.power.TreeNode;
import cn.com.jrr.core.service.power.SysResourceService;
import cn.com.jrr.core.service.power.SysUserService;



@Controller
public class Test {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysResourceService sysResourceService;

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @ResponseBody
    @Transactional
    @RequestMapping("/logincon")
    public ResponseMessage loginCon(HttpServletRequest request,SysUser sysUser){
        ResponseMessage rm=new ResponseMessage();
        HttpSession session=request.getSession();
        if(sysUser==null){
            rm.setStatus("fail");
            rm.setMessage("系统错误");
            return rm;
        }
        List<SysUser> sus=sysUserService.login(sysUser.getUserName(),sysUser.getPassword());
        if(sus.size()==1){
            sysUser=sus.get(0);
            session.setAttribute(WebKey.USER, sysUser);
            rm.setStatus("success");
        }else{
            rm.setStatus("fail");
            rm.setMessage("用户名或密码错误");
        }
        return rm;
    }

    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        HttpSession session=request.getSession();
        if(session.getAttribute(WebKey.USER)==null){
            return "login";
        }
        return "index";
    }

    @RequestMapping("/exit")
    public String exit(HttpServletRequest request){
        HttpSession session=request.getSession();
        session.setAttribute(WebKey.USER,null);
        return "login";
    }

    @ResponseBody
    @RequestMapping("/list")
    public List<TreeNode> getMenus(HttpServletRequest request){
        HttpSession session=request.getSession();
        SysUser user=(SysUser)session.getAttribute(WebKey.USER);
        List<SysResource> srs=sysResourceService.getMenu(user.getUserId());
        //List<SysResource> srs=sysResourceService.getMenu(13);
        session.setAttribute(WebKey.MENU, srs);
        List<TreeNode> treeNodes=getTreeNode(srs,1);
        return treeNodes;

    }
    private List<TreeNode> getTreeNode(List<SysResource> srs,Integer parentId){
        List<TreeNode> childrens=new ArrayList<TreeNode>();
        for(int i=0;i<srs.size();i++){
            if(!"3".equals(srs.get(i).getResourceType())){
                if(parentId==srs.get(i).getResourceParent()){
                    TreeNode tn=new TreeNode();
                    tn.setId(srs.get(i).getResourceId());
                    tn.setText(srs.get(i).getResourceName());
                    tn.setUrl(srs.get(i).getResourceUrl());
                    tn.setChildren(getTreeNode(srs,srs.get(i).getResourceId()));
                    childrens.add(tn);
                }
            }
        }
        return childrens;
    }

    @RequestMapping(value="/count",method = RequestMethod.GET)
    public String tt(int count){
        System.out.println(count);
        //System.out.println(count);
        return "index";
    }
}
